package com.aj.activity.fragment;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aj.activity.R;
import com.aj.application.LoveHomeApplication;
import com.aj.databases.dao.AccountDao;
import com.aj.databases.dao.FamilyDao;
import com.aj.databases.dao.HomeDao;
import com.aj.databases.dao.UserDao;
import com.aj.databases.entry.Account;
import com.aj.databases.entry.Family;
import com.aj.databases.entry.Home;
import com.aj.databases.entry.User;
import com.aj.http.AsynchronizationHttpRequest.IHttpCallBack;
import com.aj.http.request.HomeRequest;
import com.aj.http.response.BaseRespond;
import com.aj.http.response.Common;
import com.aj.http.response.HomeResponse;
import com.aj.utils.Contants;
import com.meg7.widget.CustomShapeSquareImageView;

/**
 * A simple {@link Fragment} subclass.
 */
public class FamilyFragment extends Fragment {

	private static final String TAG = FamilyFragment.class.getName();

	private LinearLayout myfamily_linearlayout;
	private TextView myfamily_text;
	private CustomShapeSquareImageView myfamily_image;

	private LinearLayout myfamily_myfather_linearlayout;
	private TextView myfamily_myfather;
	private CustomShapeSquareImageView myfamily_myfather_image;

	private LinearLayout myfamily_mymather_linearlayout;
	private TextView myfamily_mymather;
	private CustomShapeSquareImageView myfamily_mymother_image;

	private LinearLayout myfamily_fatherinlaw_linearlayout;
	private TextView myfamily_fatherinlaw;
	private CustomShapeSquareImageView myfamily_fatherinlaw_image;

	private LinearLayout myfamily_motherinlaw_linearlayout;
	private TextView myfamily_motherinlaw;
	private CustomShapeSquareImageView myfamily_motherinlaw_image;

	private LinearLayout myfamily_childone_linearlayout;
	private TextView myfamily_childone;
	private CustomShapeSquareImageView myfamily_childone_image;

	private LinearLayout myfamily_childtwo_linearlayout;
	private TextView myfamily_childtwo;
	private CustomShapeSquareImageView myfamily_childtwo_image;

	private LinearLayout myfamily_childthree_linearlayout;
	private TextView myfamily_childthree;
	private CustomShapeSquareImageView myfamily_childthree_image;
	private HomeDao homeDao;
	private FamilyDao familyDao;
	private List<Home> homeList;

	private View bodyView;

	public FamilyFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		homeDao = new HomeDao(getActivity());
		familyDao = new FamilyDao(getActivity());
		bodyView = inflater.inflate(R.layout.fragment_family, container, false);
		return bodyView;
	}

	@Override
	public void onResume() {
		super.onResume();
		initView();
	}

	private void initView() {
		myfamily_image = (CustomShapeSquareImageView) bodyView
				.findViewById(R.id.myfamily_image);
		myfamily_linearlayout = (LinearLayout) bodyView
				.findViewById(R.id.myfamily_linearlayout);
		myfamily_text = (TextView) bodyView.findViewById(R.id.myfamily_text);

		myfamily_myfather_linearlayout = (LinearLayout) bodyView
				.findViewById(R.id.myfamily_myfather_linearlayout);
		myfamily_myfather = (TextView) bodyView
				.findViewById(R.id.myfamily_myfather);
		myfamily_myfather_image = (CustomShapeSquareImageView) bodyView
				.findViewById(R.id.myfamily_myfather_image);

		myfamily_mymather_linearlayout = (LinearLayout) bodyView
				.findViewById(R.id.myfamily_mymather_linearlayout);
		myfamily_mymather = (TextView) bodyView
				.findViewById(R.id.myfamily_mymather);
		myfamily_mymother_image = (CustomShapeSquareImageView) bodyView
				.findViewById(R.id.myfamily_mymother_image);

		myfamily_fatherinlaw_linearlayout = (LinearLayout) bodyView
				.findViewById(R.id.myfamily_fatherinlaw_linearlayout);
		myfamily_fatherinlaw = (TextView) bodyView
				.findViewById(R.id.myfamily_fatherinlaw);
		myfamily_fatherinlaw_image = (CustomShapeSquareImageView) bodyView
				.findViewById(R.id.myfamily_fatherinlaw_image);

		myfamily_motherinlaw_linearlayout = (LinearLayout) bodyView
				.findViewById(R.id.myfamily_motherinlaw_linearlayout);
		myfamily_motherinlaw = (TextView) bodyView
				.findViewById(R.id.myfamily_motherinlaw);
		myfamily_motherinlaw_image = (CustomShapeSquareImageView) bodyView
				.findViewById(R.id.myfamily_motherinlaw_image);

		myfamily_childone_linearlayout = (LinearLayout) bodyView
				.findViewById(R.id.myfamily_childone_linearlayout);
		myfamily_childone = (TextView) bodyView
				.findViewById(R.id.myfamily_childone);
		myfamily_childone_image = (CustomShapeSquareImageView) bodyView
				.findViewById(R.id.myfamily_childone_image);

		myfamily_childtwo_linearlayout = (LinearLayout) bodyView
				.findViewById(R.id.myfamily_childtwo_linearlayout);
		myfamily_childtwo = (TextView) bodyView
				.findViewById(R.id.myfamily_childtwo);
		myfamily_childtwo_image = (CustomShapeSquareImageView) bodyView
				.findViewById(R.id.myfamily_childtwo_image);

		myfamily_childthree_linearlayout = (LinearLayout) bodyView
				.findViewById(R.id.myfamily_childthree_linearlayout);
		myfamily_childthree = (TextView) bodyView
				.findViewById(R.id.myfamily_childthree);
		myfamily_childthree_image = (CustomShapeSquareImageView) bodyView
				.findViewById(R.id.myfamily_childthree_image);
		ClickEvent clickEvent = new ClickEvent();
		myfamily_linearlayout.setOnClickListener(clickEvent);
		myfamily_myfather_linearlayout.setOnClickListener(clickEvent);
		myfamily_mymather_linearlayout.setOnClickListener(clickEvent);
		myfamily_fatherinlaw_linearlayout.setOnClickListener(clickEvent);
		myfamily_fatherinlaw_linearlayout.setOnClickListener(clickEvent);
		myfamily_motherinlaw_linearlayout.setOnClickListener(clickEvent);
		myfamily_childone_linearlayout.setOnClickListener(clickEvent);
		myfamily_childtwo_linearlayout.setOnClickListener(clickEvent);
		myfamily_childthree_linearlayout.setOnClickListener(clickEvent);
		initFamily();
	}

	private void initFamily() {
		if (LoveHomeApplication.token != null
				&& LoveHomeApplication.token.trim().length() > 0) {
			AccountDao accountDao = new AccountDao(getActivity());
			UserDao userDao = new UserDao(getActivity());
			Account account = accountDao.getByToken(LoveHomeApplication.token);
			if (account != null) {
				User user = userDao.getUserByUserId(account
						.getLast_used_user_id());
				if (user != null) {
					new HomeRequest().excute(getActivity(), user.getUser_id(),
							"50", "0", new IHttpCallBack() {
								@Override
								public void success(String result) {
									try {
										HomeResponse homeResponse = new HomeResponse(
												new JSONObject(result));
										if (homeResponse != null) {
											Common common = homeResponse
													.getCommon();
											if (common != null) {
												if (common
														.getReturnCode()
														.trim()
														.equals(BaseRespond.SUCCESS_RESULT_CODE_STRING)) {
													homeList = homeResponse
															.getHomeList();
													for (Home home : homeList) {
														home = homeDao
																.insert(home);
														if (home.getFamilyList() != null) {
															for (Family family : home
																	.getFamilyList()) {
																familyDao
																		.insert(family);
																initHomeView(family);
															}
															initChildeView();
														}
													}
												} else {
													Log.e(TAG,
															"message:"
																	+ common.getErrorMsg());
												}
											}
										}

									} catch (JSONException e) {
									}
								}

								@Override
								public void fail(String result) {
									try {
										Common common = new Common(
												new JSONObject(result));
										if (common != null) {
											Log.e(TAG,
													"message:"
															+ common.getErrorMsg());
										}
									} catch (JSONException e) {
										e.printStackTrace();
									}
								}
							});
				}
			}
		}

	}

	private void initHomeView(Family family) {
		if (family.getCallName() == null || family.getCallName().equals("null")
				|| family.getCallName().trim().length() == 0) {
			myfamily_linearlayout.setTag(family);
		} else if (family.getCallName().equals("爸爸")) {
			myfamily_myfather_linearlayout.setVisibility(View.VISIBLE);
			myfamily_myfather_linearlayout.setTag(family);
		} else if (family.getCallName().equals("妈妈")) {
			myfamily_mymather_linearlayout.setVisibility(View.VISIBLE);
			myfamily_mymather_linearlayout.setTag(family);
		} else if (family.getCallName().equals("岳父")) {
			myfamily_fatherinlaw_linearlayout.setVisibility(View.VISIBLE);
			myfamily_fatherinlaw_linearlayout.setTag(family);
		} else if (family.getCallName().equals("岳母")) {
			myfamily_motherinlaw_linearlayout.setVisibility(View.VISIBLE);
			myfamily_motherinlaw_linearlayout.setTag(family);
		}
		// TODO
		// else if (family.getCallName().equals("女儿")
		// || family.getCallName().equals("儿子")) {

	}

	private void initChildeView() {
		List<Family> tempFamilyList = new ArrayList<Family>();
		List<Family> boysList = familyDao.queryFamilyByCallName("儿子");
		if (boysList != null && boysList.size() > 0) {
			tempFamilyList.addAll(boysList);
		}
		List<Family> girlsList = familyDao.queryFamilyByCallName("女儿");
		if (girlsList != null && girlsList.size() > 0) {
			tempFamilyList.addAll(girlsList);
		}
		List<Family> childList = familyDao.queryFamilyByCallName("胎儿");
		if (childList != null && childList.size() > 0) {
			tempFamilyList.addAll(childList);
		}
		if (tempFamilyList.size() == 1) {
			myfamily_childone_linearlayout.setVisibility(View.VISIBLE);
			myfamily_childone_linearlayout.setTag(tempFamilyList.get(0));
		} else if (tempFamilyList.size() == 2) {
			myfamily_childone_linearlayout.setVisibility(View.VISIBLE);
			myfamily_childone_linearlayout.setTag(tempFamilyList.get(0));
			myfamily_childtwo_linearlayout.setVisibility(View.VISIBLE);
			myfamily_childtwo_linearlayout.setTag(tempFamilyList.get(1));
		} else if (tempFamilyList.size() >= 3) {
			myfamily_childone_linearlayout.setVisibility(View.VISIBLE);
			myfamily_childone_linearlayout.setTag(tempFamilyList.get(0));
			myfamily_childtwo_linearlayout.setVisibility(View.VISIBLE);
			myfamily_childtwo_linearlayout.setTag(tempFamilyList.get(1));
			myfamily_childthree_linearlayout.setVisibility(View.VISIBLE);
			myfamily_childthree_linearlayout.setTag(tempFamilyList.get(2));
		}
	}

	class ClickEvent implements OnClickListener {
		@Override
		public void onClick(View v) {
			Intent intent = null;
			switch (v.getId()) {
			case R.id.myfamily_linearlayout:
				if (LoveHomeApplication.token == null
						|| LoveHomeApplication.token.trim().length() == 0) {
					intent = new Intent(Contants.LOGINACTIVITY_ACTION);
				} else {
					intent = new Intent(Contants.FAMILYSPACE_ACTIVITY_ACTION);
					intent.putExtra(Contants.FAMILY_BEAN_PARAMS,
							(Family) v.getTag());
					intent.putExtra(Contants.SELF_PAGE_FLAG,
							Contants.ALBUM_TYPE_SELF);
				}
				startActivity(intent);
				break;
			case R.id.myfamily_myfather_linearlayout:
			case R.id.myfamily_mymather_linearlayout:
			case R.id.myfamily_fatherinlaw_linearlayout:
			case R.id.myfamily_motherinlaw_linearlayout:
			case R.id.myfamily_childone_linearlayout:
			case R.id.myfamily_childtwo_linearlayout:
			case R.id.myfamily_childthree_linearlayout:
				intent = new Intent(Contants.FAMILYSPACE_ACTIVITY_ACTION);
				intent.putExtra(Contants.FAMILY_BEAN_PARAMS,
						(Family) v.getTag());
				intent.putExtra(Contants.SELF_PAGE_FLAG,
						Contants.ALBUM_TYPE_OTHER);
				startActivity(intent);
				break;
			default:
				break;
			}
		}

	}
}
